package com.exartisan.util;

import java.util.HashMap;

import com.exartisan.network.FacebookResponseParser;
import com.exartisan.network.FbConnnector;
import com.exartisan.network.NetworkUtility;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;

public class Util {

	public static void hideSoftKeyboard(Activity c) {
		InputMethodManager inputMethodManager = (InputMethodManager) c
				.getSystemService(Activity.INPUT_METHOD_SERVICE);
		inputMethodManager.hideSoftInputFromWindow(c.getCurrentFocus()
				.getWindowToken(), 0);
	}

	public static void showToast(String msg, Context c, int length) {
		Toast.makeText(c, msg, length).show();
	}

	public static void setCommentCount(final String id, final TextView tv) {
		FbConnnector.get(id, NetworkUtility.CONNECTION_COMMENT,
				new RequestParams(NetworkUtility.SUMMARY, 1),
				new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(int arg0, String response) {
						super.onSuccess(arg0, response);
						Log.d("count", response);
						int count = FacebookResponseParser.getCount(response);
						if (count != 0)
							tv.setText(count + "");
					}

					@Override
					public void onFailure(Throwable arg0, String arg1) {
						super.onFailure(arg0, arg1);
						// Log.d("fail", "fail");
					}

				});
	}

	public static void setLikeCount(final String id, final TextView tv) {
		FbConnnector.get(id, NetworkUtility.CONNECTION_LIKE, new RequestParams(
				NetworkUtility.SUMMARY, 1), new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(int arg0, String response) {
				super.onSuccess(arg0, response);
				Log.d("count", response);
				int count = FacebookResponseParser.getCount(response);
				if (count != 0)
					tv.setText(count + "");
			}

			@Override
			public void onFailure(Throwable arg0, String arg1) {
				super.onFailure(arg0, arg1);
				// Log.d("fail", arg1);
			}

		});
	}

	public static void setCommentCount(final String id, final TextView tv,
			final HashMap<String, Integer> commentCount) {
		FbConnnector.get(id, NetworkUtility.CONNECTION_COMMENT,
				new RequestParams(NetworkUtility.SUMMARY, 1),
				new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(int arg0, String response) {
						super.onSuccess(arg0, response);
						Log.d("count", response);
						int count = FacebookResponseParser.getCount(response);
						if (count != 0)
							tv.setText(count + "");
						else tv.setText("");
						commentCount.put(id, count);
						
					}

					@Override
					public void onFailure(Throwable arg0, String arg1) {
						super.onFailure(arg0, arg1);
						Log.d("fail", "fail");
					}

				});
	}

	public static void setLikeCount(final String id, final TextView tv,
			final HashMap<String, Integer> likeCount) {
		FbConnnector.get(id, NetworkUtility.CONNECTION_LIKE, new RequestParams(
				NetworkUtility.SUMMARY, 1), new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(int arg0, String response) {
				super.onSuccess(arg0, response);
				Log.d("count", response);
				int count = FacebookResponseParser.getCount(response);
				if (count != 0) 
					tv.setText(count + "");
				else tv.setText("");
					likeCount.put(id, count);
				
			}

			@Override
			public void onFailure(Throwable arg0, String arg1) {
				super.onFailure(arg0, arg1);
				// Log.d("fail", arg1);
			}

		});
	}

}
